<?php
 /*Template Name: Project
 */

 	// Get construction update
	$content = get_the_content();
	$original_permalink = get_permalink();
	$original_id = get_the_ID();
	$original_update_date = get_post_meta(get_the_ID(), 'cons_update_date', true);

	// Get project id
	$project_id = get_query_var('p_id');
	if ( empty($project_id) ){
		echo 'fasdf';
		$query_args = array(
			'connected_type' => 'projects_to_consupdate',
			'connected_items' => intval(get_the_ID()),        
			'nopaging' => true
		);
		$project_query = new WP_Query($query_args);
		if ( $project_query->have_posts() ){
			$project_query->the_post();
			wp_redirect($original_permalink . '?p_id=' . get_the_ID(), 301);
			//wp_redirect('/not-found/', 404);
			exit;
		}
		else {
			echo get_site_url(219);
			//wp_redirect(get_site_url('1-a'), 301);
		}
		
	}
	get_header(); 
?>

<div class="container" id="top_info">
	<div class="top-content">
		
    	<?php
		
			
			
			
			
			// Get main post
			$query_args = array(
				'connected_type' => 'projects_to_consupdate',
				'connected_items' => intval($original_id),   
				'connected_direction' => 'from',    
				'nopaging' => true
			);
			$project_query = new WP_Query($query_args);
				
			if($project_query->have_posts()):
				$post_id = get_the_ID();
				$project_query->the_post(); 
				$project_overview = get_post_meta($post->ID, 'dmci_project_overview', true); 
				$location_and_vicinity = get_post_meta($post->ID, 'dmci_location_vicinity', true);
				$building_features = get_post_meta($post->ID, 'dmci_building_features_and_plans', true);
				$unit_features = get_post_meta($post->ID, 'dmci_unit_features_and_plans', true);
				$amenities = get_post_meta($post->ID, 'dmci_amenities', true);
				
				$sliders = get_post_meta($post->ID, 'dmci_sliders', true);
				$slider_title = get_post_meta($post->ID, 'dmci_slider_title', true);
				$slider_desc = get_post_meta($post->ID, 'dmci_slider_desc', true);
				$permalink = get_permalink();
			endif;
			
			
			
		
			
			
			$img = get_post_meta($post->ID, 'dmci_project_logo', true); 
			$imgH = get_post_meta($post->ID, 'logoHeight', true); 
			$imgW = get_post_meta($post->ID, 'logoWidth', true);
			
			
			
			if( $img ) {
				echo '<img src="'.$img.'" width="'.$imgW.'px" height="'.$imgH.'px" class="proj-logo" alt="'.get_the_title().' Logo"/>';	
			}
		?>	
			<ul class="proj-nav">
				
				<?php if( $project_overview ) :?> 
				<li>
					<a href="<?php echo $permalink; ?>" class="view-target">
						<span class="arrow">></span>
						<span class="nav-text">Project Overview</span>
					</a>
				</li>
				<?php endif; ?>
				
				<?php if( $location_and_vicinity ) :?>
				<li>
					<a href="<?php echo $permalink.'?location-vicinity'; ?>" class="view-target">
						<span class="arrow">></span>
						<span class="nav-text">Location and Vicinity</span>
					</a>
				</li>
				<?php endif; ?>
				
				<?php if( $building_features ) :?>
				<li>
					<a href="<?php echo $permalink.'?building-features'; ?>" class="view-target">
						<span class="arrow">></span>
						<span class="nav-text">Building Features and Plans</span>
					</a>
				</li>
				<?php endif; ?>
				
				<?php if( $unit_features ) :?>
				<li>
					<a href="<?php echo $permalink.'?unit-features'; ?>" class="view-target">
						<span class="arrow">></span>
						<span class="nav-text">Unit Features and Plans</span>
					</a>
				</li>
				<?php endif; ?>
				
				<?php if( $amenities ) :?>
				<li>
					<a href="<?php echo $permalink.'?amenities'; ?>" class="view-target">
						<span class="arrow">></span>
						<span class="nav-text">Amenities</span>
					</a>
				</li>
				<?php endif; ?>
				
				<li>
					<a href="<?php echo $original_permalink . '?p_id=' . $project_id; ?>" class="view-target active">
						<span class="arrow">></span>
						<span class="nav-text">Construction Updates</span>
					</a>
				</li>
				
			</ul>		
		<div class="clear">&nbsp;</div>
    </div>
    <div class="top-curve">&nbsp;</div>
</div>


<div class="container">
	<?php 
		
		if( $sliders ):
		$sliders = json_decode(htmlspecialchars_decode($sliders), true);
		
	?>
	<div class="page-slider-wrapper">
    <div class="page-slider">
		
		<?php foreach($sliders as $slide) :?>
        <div class="slide">
        	<img src="<?php echo $slide; ?>" alt="Image Slider" />
        </div>		
		<?php endforeach;?>
        
    </div>
	<div class="slider-text">
        <div class="title"><?php echo $slider_title; ?></div>
     	<div class="desc"><?php echo $slider_desc; ?></div>
    </div>
    </div>
	<?php else: ?>
		<div class="page-filler">&nbsp;</div>
	<?php endif; ?>
	
    
    <div class="page-content padded">
		<div class="view">
		<h2 class="update-heading">Construction Updates</h2>
    	<?php 
			echo apply_filters("the_content", $content);
		?>
		
		<?php
			$cons_slides = get_post_meta($original_id , 'cons_slide', true);
			
			if( !empty($cons_slides) ):
		?>
        	<div class="cons-slider">
            	<div class="main-image">
                	<?php $ctr = 0; foreach( $cons_slides as $slide) : ?>
                    <div class="img-slide" id="cons_slide<?php echo $ctr; ?>">
                    	<img src="<?php echo $slide['image']; ?>" />
						<p><strong><?php echo $original_update_date; ?></strong><?php echo $slide['text']; ?></p>
                    </div>
                    <?php $ctr++; endforeach;?>
                </div>
                <div class="cons-slider-nav">
                	<div class="prev disabled"><a href="#" class="prev-bnt"><</a></div>
                    <div class="wrapper">
                    	<div class="scroll">
                        	<?php $ctr = 0; foreach( $cons_slides as $slide ) : ?>
                            <a href="#cons_slide<?php echo $ctr; ?>">
                            	<img src="<?php echo $slide['image']; ?>" width="140" height="120" alt="<?php echo $slide['text']; ?>"/>
                            </a>
                            <?php $ctr++; endforeach; ?>
                        </div>
                    </div>
                    <div class="next"><a href="#" class="next-btn">></a></div>
                </div>
            </div>
        <?php endif; ?>
        
		<div class="other-updates">
			<h4>Other Updates</h4>
			<?php
			// Get related construction updates
			$query_args = array(
					'connected_type' => 'projects_to_consupdate',
					'post_type' => 'dmci_cons_update',
					'connected_direction' => 'to',
					'connected_its' => $post_id,
					'nopaging' => true,
					'meta_key' => 'cons_update_date',
					'orderby' => 'meta_value',
					'order' => 'DESC'
				);
			$project_query = new WP_Query($query_args);
			if( $project_query->have_posts() ): ?>
			<ul>
			<?php
			while( $project_query->have_posts() ) : $project_query->the_post();
				if( (int)$post->p2p_to == (int)$project_id ) : 
					$the_date = get_post_meta($post->ID, 'cons_update_date', true);
				?>
					<li><a href="<?php echo get_permalink() . '?p_id=' . $project_id ?>"><?php echo $the_date ? date("F j, Y", strtotime($the_date) ) : ''; ?></a></li>
				<?php endif;
			endwhile;
			?>
			</ul>
			<?php
			endif;
			?>
			<div class="clear">&nbsp;</div>
		</div>
		
		</div>
	</div>
 
</div>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>